package com.example.mybatisplus.config;

import com.example.mybatisplus.threadPool.InheritableThreadPoolTaskExecutor;
import com.example.mybatisplus.threadPool.ThreadPoolExecutors;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ThreadPoolExecutor;

/**
 * @description:
 * @author: dengPing.xu
 * @date: 2022-03-19
 */
@Configuration
public class ThreadConfig {

	@Bean("threadPoolExecutors")
	public ThreadPoolTaskExecutor getThreadPoolExecutors(){
		ThreadPoolExecutors threadPoolExecutors = new ThreadPoolExecutors();
		return threadPoolExecutors;
	}

	@Bean(name = "assignThreadPoolTaskExecutor")
	public ThreadPoolTaskExecutor assignThreadPoolTaskExecutor(){
		ThreadPoolTaskExecutor assignThreadPoolTaskExecutor = new InheritableThreadPoolTaskExecutor();
		//核心线程数量
		assignThreadPoolTaskExecutor.setCorePoolSize(20);
		//最大线程数量
		assignThreadPoolTaskExecutor.setMaxPoolSize(20);
		//队列中最大任务数
		assignThreadPoolTaskExecutor.setQueueCapacity(5);
		//线程名称前缀(自动派单)
		assignThreadPoolTaskExecutor.setThreadNamePrefix("AssignThreadPool-");
		//当达到最大线程数时如何处理新任务
		assignThreadPoolTaskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
		//线程空闲后最大存活时间
		assignThreadPoolTaskExecutor.setKeepAliveSeconds(60);
		//初始化线程池
		assignThreadPoolTaskExecutor.initialize();
		return assignThreadPoolTaskExecutor;
	}
}
